package com.mofofa.maozy.me

import android.annotation.SuppressLint
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.RecyclerView
import com.mofofa.maozy.R
import com.mofofa.maozy.components.dialog.PopListItem
import com.mofofa.maozy.components.form.*

class FeedbackTypeAdapter(private val dataList: List<PopListItem>) :
    RecyclerView.Adapter<FeedbackTypeAdapter.ViewHolder>() {

    private var mMaozyOnMaozyActionMaozyClickMaozyListener: (Int, PopListItem) -> Unit =
        { index, item -> }

    inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        var textFeedbackTypeTitle: TextView =
            view.findViewById(R.id.textFeedbackTypeTitle)
    }

    override fun onCreateViewHolder(
        parent: ViewGroup,
        viewType: Int,
    ): ViewHolder {
        val view = LayoutInflater.from(parent.context)
            .inflate(R.layout.view_maozy_adpter_feedback_type, parent, false)
        return ViewHolder(view)
    }

    @SuppressLint("SetTextI18n")
    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val data: PopListItem = dataList[position]
        holder.textFeedbackTypeTitle.text = data.display
        if (data.isChecked) {
            holder.textFeedbackTypeTitle.setBackgroundResource(R.drawable.maozy_feedback_type_selected)
            holder.textFeedbackTypeTitle.setTextColor(
                ContextCompat.getColor(
                    holder.textFeedbackTypeTitle.context,
                    R.color.main_red
                )
            )
        } else {
            holder.textFeedbackTypeTitle.setBackgroundResource(R.drawable.maozy_feedback_type_unselect)
            holder.textFeedbackTypeTitle.setTextColor(
                ContextCompat.getColor(
                    holder.textFeedbackTypeTitle.context,
                    R.color.black_text_light
                )
            )
        }
        holder.textFeedbackTypeTitle.setOnClickListener {
            mMaozyOnMaozyActionMaozyClickMaozyListener.invoke(position, data)
        }
    }

    override fun getItemCount(): Int {
        return dataList.size
    }

    fun setMaozyOnMaozyItemMaozyClickMaozyListener(listener: (Int, PopListItem) -> Unit) {
        mMaozyOnMaozyActionMaozyClickMaozyListener = listener
    }
}